/**
 * @constructor
 * 
 * @param canvas
 * @param sprites
 */
function InputHandler(canvas, sprites) {
	
	this.canvas = canvas;
	this.sprites = sprites;
	
	this.oldMouseX = -1;
	this.oldMouseY = -1;
	
	this.isMouseDown = false;
	
	this.startListening = function() {
		var self = this;
		self.canvas.mousemove(function(e){
			newMouseX = e.pageX-self.canvas.position().left;
			newMouseY = e.pageY-self.canvas.position().top;
			
			$.each(self.sprites,function(index) {
				sprite = this;
				if(sprite.contains(self.oldMouseX, self.oldMouseY) && sprite.contains(newMouseX, newMouseY)) {
					sprite.onMouseMove(newMouseX, newMouseY, self.isMouseDown);
			    } else if(!sprite.contains(self.oldMouseX, self.oldMouseY) && sprite.contains(newMouseX, newMouseY)) {
			    	sprite.onMouseOver(newMouseX, newMouseY, self.isMouseDown);
			    } else if(sprite.contains(self.oldMouseX, self.oldMouseY) && !sprite.contains(newMouseX, newMouseY)) {
			    	sprite.onMouseOut(newMouseX, newMouseY, self.isMouseDown);
			    }			    
			});
			
			self.oldMouseX = newMouseX;
			self.oldMouseY = newMouseY;
			
		});
		
		self.canvas.mouseup(function(e) {
			self.isMouseDown = false;
		});
		
		self.canvas.mousedown(function(e){
			self.isMouseDown = true;
			mouseX = e.pageX-self.canvas.position().left;
			mouseY = e.pageY-self.canvas.position().top;
			
			$.each(self.sprites,function(index) {
				sprite = this;
				if(sprite.contains(mouseX, mouseY)) {
					sprite.onMouseDown(mouseX, mouseY);
			    } 		    
			});;
			
		});
	};

}